<?php
/**
 * Created by b17
 * 2/16/12 5:33 PM
 */

class Admin_NewsController extends G4T_CrudController
{
    const NEWS_PER_PAGE = 10;

    public function indexAction()
    {
        $page = (int) $this->_getParam('page');
        $offset = $page > 0 ? ($page - 1) * self::NEWS_PER_PAGE : 0;
        $this->manager->setLimit(self::NEWS_PER_PAGE);
        $this->manager->setOffset($offset);
        $order = $this->_getParam('order');
        $this->view->order = $order;
        $items = $this->manager->getList($this->_getParam('column'), $order);
        $this->view->items = $items;
        $total = $this->manager->getCount();
        $this->displayPaginantor($total, $page, self::NEWS_PER_PAGE);
        $this->saveCurrentUrl();
    }

    /**
     * Возвращает менеджер для работы с сущностью.
     *
     * @return \Managers\CrudManager
     */
    protected function getManager()
    {
        return new \Managers\News();
    }

    /**
     * Возвращает строителя форм для создания форм.
     * @return \G4T\Form\FormBuilder
     */
    protected function createFormBuilder()
    {
        $rubricsManager = new \Managers\Rubrics();
        $issueManager = new \Managers\Issue();
        $formBuilder = \G4T\Form\FormBuilder::createFormBuilder(\Managers\News::TABLE)
            ->addField('title', 'string', array('label' => 'Title', 'class' => 'medium-input'))
            ->addField('introduction', 'string', array('label' => 'Introduction', 'class' => 'text-input large-input'))
            ->addField('content', 'text', array('label' => 'Content'))
            ->addField('author', 'string', array('label' => 'Author', 'class' => 'text-input small-input'))
            ->addField('source', 'string', array('label' => 'Source', 'class' => 'text-input medium-input', 'required' => false))
            ->addField('auto', 'string', array('label' => 'Tags', 'id' => 'tags', 'class' => 'medium-input')) // теги
            ->addField('img', 'image', array('label' => 'Image', 'required' => false))
            ->addField('issue', 'select', array('label' => 'Issue', 'values' => $issueManager->getSelectList()))
            ->addField('public', 'boolean', array('label' => 'Public'))
            ->addField('top', 'boolean', array('label' => 'Top'))
            ->addField('allowComments', 'boolean', array('label' => 'Allow comments'))
            ->addField('rubric', 'select', array('label' => 'Rubric', 'values' => $rubricsManager->getSelectList()))
            ->addField('token', 'hash')
            ->addField('submit', 'submit', array('label' => 'Save', 'class' => 'btn btn-primary'));
        return $formBuilder;
    }

    /**
     * Переход при не найденой сущности.
     * @param $id
     */
    protected function redirectWhenNotFound($id = null)
    {
        $this->_flashMessenger->addMessage('Record not found', Lib_Flash::ERROR, Lib_Flash::LATER);
        $url = $this->getSavedUrl() ? : '/admin/news';
        $this->_redirect($url);
    }

    /**
     * Переход при успешном изменении объекта.
     * @param $entity
     */
    protected function redirectWhenSuccess($entity = null)
    {
        $this->_flashMessenger->addMessage('Changes applied.', Lib_Flash::NOTICE, Lib_Flash::LATER);
        $url = $this->getSavedUrl() ? : '/admin/news';
        $this->_redirect($url);
    }

    protected function getDefaultValues()
    {
        $values = array('rubric' => $this->_getParam('rubric'), 'allowComments' => 1);
        return $values;
    }

    protected function decorate($record)
    {
        $decorator = new \Managers\Decorators\Autocomplete();
        $record['auto'] = join(',', $decorator->decorate($record['tags']));
        return $record;
    }


}
